package com.relman.model
{
    import mx.collections.ArrayCollection;
    import mx.collections.ICollectionView;
    import mx.collections.IViewCursor;
    import mx.collections.XMLListCollection;
    import mx.formatters.CurrencyFormatter;

	import com.adobe.cairngorm.model.ModelLocator;
	
	import com.relman.vo.*;
	import com.relman.util.ServerErrors;
	import com.relman.util.DebugUtils;
   
	[Bindable]
	public class RelmanModelLocator implements ModelLocator {
		private static var modelLocator : RelmanModelLocator;
      
    public static function getInstance() : RelmanModelLocator {
    	if ( modelLocator == null ) {
      	modelLocator = new RelmanModelLocator();
      }
			return modelLocator;
		}
      
		//Constructor should be private but current AS3.0 does not allow it yet (?)...
		public function RelmanModelLocator() {	
			if ( modelLocator != null ) {
				throw new Error( "Only one RelmanModelLocator instance should be instantiated" );	
			}
		}
   
		/* bindables go here */
		public var currentUser : UserVO;
		
		public var currentTransaction : TransactionVO;
		
		private var _currentTransactionHistoryXLC : XMLListCollection;
		
		public var selectedManager : ReleaseManagerVO;
		
		public var selectedProject : ProjectVO;
		
		public var selectedEnvironment : EnvironmentVO;
		
		private var _managersXLC : XMLListCollection;
		
		private var _projectsXLC : XMLListCollection;
		
		private var _environmentsXLC : XMLListCollection;
		
		public var managerMap : Object;
		
		public var projectMap : Object;
		
		public var environmentMap : Object;
		
		public var currentTransactionHistoryMap : Object;
		
		public function get currentTransactionHistoryXLC() : XMLListCollection {
			return _currentTransactionHistoryXLC;
		}
		
		public function set currentTransactionHistoryXLC(setValue : XMLListCollection) : void {
			_currentTransactionHistoryXLC = setValue;
			updateCurrentTransHistoryMap();
		}
		
		public function get managersXLC() : XMLListCollection {
			return _managersXLC;
		}
		
		public function set managersXLC(setValue : XMLListCollection) : void {
			_managersXLC = setValue;
			updateManagersMap();
		}
		
        public function get projectsXLC() : XMLListCollection {
            return _projectsXLC;
        }
        
        public function set projectsXLC(setValue : XMLListCollection) : void {
            _projectsXLC = setValue;
            updateProjectsMap();
        }
        
        public function get environmentsXLC() : XMLListCollection {
        	return _environmentsXLC;
        }
        
        public function set environmentsXLC(setValue : XMLListCollection) : void {
        	_environmentsXLC = setValue;
        	updateEnvironmentsMap();
        }
        
        public function updateManagersMap() : void {
            var mgrCursor:IViewCursor = managersXLC.createCursor();
            var tempMap:Object = {};
            while (!mgrCursor.afterLast) {
                var mgr_xml:XML = XML(mgrCursor.current);
                //DebugUtils.debug("in iter of projects cursor");
                //DebugUtils.debug(project_xml.toXMLString());
                var manager : ReleaseManagerVO = ReleaseManagerVO.fromXML(mgr_xml);
                tempMap[mgr_xml.name] = manager;
                mgrCursor.moveNext();
            }
            managerMap = tempMap;
        }
        
        public function updateProjectsMap() : void {
            var projectsCursor:IViewCursor = projectsXLC.createCursor();
            var tempProjectIdMap:Object = {};
            while (!projectsCursor.afterLast) {
                var project_xml:XML = XML(projectsCursor.current);
                //DebugUtils.debug("in iter of projects cursor");
                //DebugUtils.debug(project_xml.toXMLString());
                var project : ProjectVO = ProjectVO.fromXML(project_xml);
                tempProjectIdMap[project_xml.name] = project;
                projectsCursor.moveNext();
            }
            projectMap = tempProjectIdMap;
        }
        
        public function updateEnvironmentsMap() : void {
            var envCursor:IViewCursor = environmentsXLC.createCursor();
            var tempMap:Object = {};
            while (!envCursor.afterLast) {
                var env_xml:XML = XML(envCursor.current);
                var env : EnvironmentVO = EnvironmentVO.fromXML(env_xml);
                tempMap[env_xml.name] = env;
                envCursor.moveNext();
            }
            environmentMap = tempMap;
        }
        
        public function updateCurrentTransHistoryMap() : void {
            var histCursor:IViewCursor = currentTransactionHistoryXLC.createCursor();
            var tempMap:Object = {};
            var count : uint = 0;
            while (!histCursor.afterLast) {
                var hist_xml:XML = XML(histCursor.current);
                var hist : TransHistoryVO = TransHistoryVO.fromXML(hist_xml);
                tempMap[count] = hist;
                count += 1;
                histCursor.moveNext();
            }
            currentTransactionHistoryMap = tempMap;
        }

	} // end class
} // end package
